//
//  LSZInformationSubinterfaceVC.m
//  PartyClassCloud
//
//  Created by 梁素珍 on 2018/8/17.
//  Copyright © 2018年 九酷. All rights reserved.
//

#import "LSZInformationSubinterfaceVC.h"

@interface LSZInformationSubinterfaceVC ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong) UITableView * tableViewInformation;
@property(nonatomic,strong) NSMutableArray * mutArrayData;
@property(nonatomic,assign) NSInteger page;
@end

@implementation LSZInformationSubinterfaceVC
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}
-(void)addNewData:(NSInteger)page
{
    [LSZRequestDicData initWithDirectoryDetails:self.catalogId page:page :^(BOOL isOk, NSString * string, LSZArticleDetailsManager * mg) {
        if (isOk == YES) {
            if(page == 0){
                [self.mutArrayData removeAllObjects];
                 [self.mutArrayData addObjectsFromArray:mg.mutMgArray];
            }else{
                [self.mutArrayData addObjectsFromArray:mg.mutMgArray];
            }
            [self.tableViewInformation reloadData];
        }else{
//            if ([string isEqualToString:@"重复登录"]&&page==0) {
//                [LSZPublicClassDataToll RepeatLoginInterfaceJump:self.view fromVC:@"LSZInformationSubinterfaceVC"];
//            }else{
//                [LSZPublicClassDataToll initWithAlertKuangView:self.view string:string];
//            }
            [LSZPublicClassDataToll initWithAlertKuangView:self.view string:string];
        }
    }];
}
-(void)goToLogin
{
    [LSZPublicClassDataToll goToLogin];
}
-(void)addNotificationCenter
{
    NSString * strDelete = [NSString stringWithFormat:@"%@Login",@"LSZInformationSubinterfaceVC"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goToLogin) name:strDelete object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    __weak typeof(self) weakSelf = self;
    [self addNotificationCenter];
    weakSelf.page = 0;
    weakSelf.mutArrayData = [[NSMutableArray alloc]init];
    [weakSelf addNewData:weakSelf.page];
    [weakSelf.tableViewInformation registerClass:[LSZArticleHeaderTableViewCell class] forCellReuseIdentifier:@"LSZArticleHeaderTableViewCell"];
    [weakSelf.tableViewInformation registerClass:[LSZImageViewAndLaeblTableViewCell class] forCellReuseIdentifier:@"LSZImageViewAndLaeblTableViewCell"];
    /*
    [weakSelf.tableViewInformation addRefreshHeaderWithHandle:^{
        NSLog(@"开始刷新");
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格  和结束刷新
            weakSelf.page = 0;
            [weakSelf addNewData:weakSelf.page];
            [weakSelf.tableViewInformation.mj_header endRefreshing];
        });
    }];
    weakSelf.tableViewInformation.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //延时
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            weakSelf.page++;
            [weakSelf addNewData:weakSelf.page];
            [weakSelf.tableViewInformation.mj_footer endRefreshing];
        });
    }];
    */
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mutArrayData.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 200;
    }
    return 100;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        LSZArticleHeaderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZArticleHeaderTableViewCell" forIndexPath:indexPath];
        if (self.mutArrayData.count > indexPath.row) {
            LSZArticleDetailsMg * mg = nil;
            mg = self.mutArrayData[indexPath.row];
            [cell.imageViewHeader sd_setImageWithURL:[NSURL URLWithString:mg.coverUrl] placeholderImage:[UIImage imageNamed:@"默认"]];
            cell.labelTitle.text = mg.name;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        LSZImageViewAndLaeblTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZImageViewAndLaeblTableViewCell" forIndexPath:indexPath];
        if (self.mutArrayData.count > indexPath.row) {
            LSZArticleDetailsMg * mg = nil;
            mg = self.mutArrayData[indexPath.row];
            cell.manager = mg;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.mutArrayData.count > indexPath.row) {
        LSZArticleDetailsMg * mg = nil;
        mg = self.mutArrayData[indexPath.row];
        LSZDirectoryDetailsViewController * vc = [[LSZDirectoryDetailsViewController alloc]init];
        vc.url = mg.contentUrl;
        vc.productID = mg.id_id;
        vc.imageViewStr = mg.coverUrl;
        vc.titleStr = mg.name;
        vc.publishTime = mg.publishTime;
        vc.period = @"0";
        [self.navigationController pushViewController:vc animated:YES];
    }
}
-(UITableView *)tableViewInformation
{
    if (_tableViewInformation == nil) {
        _tableViewInformation = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-TAB_BAR_HEIGHT-44-NAVIGATION_BAR_HEIGHT) style:UITableViewStyleGrouped];
        _tableViewInformation.delegate = self;
        _tableViewInformation.dataSource = self;
        _tableViewInformation.estimatedRowHeight=0;
        _tableViewInformation.estimatedSectionFooterHeight =0;
        _tableViewInformation.estimatedSectionHeaderHeight =0;
        [_tableViewInformation setSeparatorInset:UIEdgeInsetsMake(0, 20, 0, 20)];
        [self.view addSubview:_tableViewInformation];
    }
    return _tableViewInformation;
}
@end
